package service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;

import dao.BookDAO;
import entity.CartItem;
import entity.Product;
import entity.User;
import service.CartService;
import util.CookieUtil;

public class CartServiceImpl implements CartService{
	private BookDAO bookDao;
	public void setBookDao(BookDAO bookDao) {
		this.bookDao = bookDao;
	}
	
	public void cartInit() throws Exception{
		System.out.println("init");
		Map<String,Object> session = ActionContext.getContext().getSession();
		HttpServletRequest httpReq = ServletActionContext.getRequest();
		User user = (User)session.get("user");
		System.out.println(user!=null);
		if (user!=null) {
			String uid = user.getId()+"";
			System.out.println(uid);
			String value = CookieUtil.findCookie(uid, httpReq);
			if (value!=null) {
				this.load(value);
			}
		}
//		session.put(Constant.CART, this);
	}
	
	private List<CartItem> store = new ArrayList<CartItem>();
	public boolean buy(int pid) throws Exception{
		//如果是第一次购买
//		for (CartItem item : store) {
//			if (item.getPro().getId()==pid) {
//				return false;
//			}
//		}
		for (int i = 0; i < store.size(); i++) {
			CartItem item = store.get(i);
			if (item.getPro().getId()==pid) {
				return false;
			}
		}
		Product p = bookDao.findProById(pid);
		CartItem item = new CartItem();
		item.setPro(p);
		store.add(item);
		return true;
	}
	
	public void del(int pid){
		for (CartItem item : store) {
			if (item.getPro().getId()==pid) {
				item.setBuy(false);
			}
		}
	}
	
	public void recover(int pid){
		for (CartItem item : store) {
			if (item.getPro().getId()==pid) {
				item.setBuy(true);
			}
		}
	}
	
	public void modify(int pid,int qty){
		for (CartItem item : store) {
			if (item.getPro().getId()==pid) {
				item.setQty(qty);
			}
		}
	}
	
	//查询总价
	public double cost(){
		double sum=0;
		for(int i=0;i<store.size();i++){
			CartItem curr = store.get(i);
			System.out.println(curr);
			if (curr.isBuy()==true) {
				sum+=curr.getPro().getDangPrice()*curr.getQty();
			}
		}
		return sum;
	}
	//折扣多少
	public double cutPrice(){
		double sum=0;
		for(int i=0;i<store.size();i++){
			CartItem curr = store.get(i);
			if (curr.isBuy()==true) {
				sum+=(curr.getPro().getFixedPrice()-curr.getPro().getDangPrice())*curr.getQty();
			}
		}
		return sum;
	}
	
	public void clear(){
		store.clear();
	}
	/**
	 * 将cart中保存的商品转换成一个字符串
	 * 字符串格式类似于: 1,8;2,11;3,8
	 * 如果没有任何商品，返回0
	 * @return
	 */
	public String store(){
		StringBuffer buf = new StringBuffer();
		if(store.size()==0){
			buf.append("0");
		}else{
			for(int i=0;i<store.size();i++){
				CartItem item = store.get(i);
				System.out.println("item"+item);
				if (item.isBuy()==true) {
					buf.append(item.getPro().getId()+","+item.getQty()+";");
				}
			}
		}
		if(buf.length()>1){
			buf.deleteCharAt(buf.length()-1);
		}
		return buf.toString();
	}
	/**
	 * 依据content内容重新恢复cart中的商品信息。
	 * @param content 类似于: 1,8;2,11;3,8
	 */
	public void load(String str){
		if(str.equals("0") || str==null){
			return;
		}
		String[] s = str.split(";");
		for(int i=0;i<s.length;i++){
			String[] str1 = s[i].split(",");
			int id = Integer.parseInt(str1[0]);
			int qty=Integer.parseInt(str1[1]);
			CartItem item = new CartItem();
			Product p;
			try {
				p = bookDao.findProById(id);
				item.setPro(p);
				item.setQty(qty);
				store.add(item);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	@Override
	public List<CartItem> getProds(boolean exist) {
		List<CartItem> items = new ArrayList<CartItem>();
		for (CartItem item : store) {
			if (item.isBuy()==exist) {
				items.add(item);
			}
		}
		return items;
	}
}
